package controller;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.activiti.engine.task.Task;

import com.web.util.Partners;
import com.web.util.WebUtils;

import wfm.activiti.ActivitiManager;
import wfm.entities.Partner;
import wfm.entities.Ticket;
import wfm.entities.User;
import wfm.entities.WFInfo;
import wfm.query.QueryDao;

@ManagedBean(name="createRepairTicketController")
@SessionScoped
public class CreateRepairTicketController {
	
	@ManagedProperty(value = "#{user}")
	private User user;
	
	@ManagedProperty(value = "#{wfInfo}")
	private WFInfo wfInfo;
	
	@ManagedProperty(value="#{selectedPartner}")
	private String selectedPartner;
	
	@ManagedProperty(value="#{typeOfDamage}")
	private String typeOfDamage;

	private List<Partner> partners=Partners.getPartners("1"); 
	private Ticket ticket=null;
	
	
	@PostConstruct
	public void init(){
		this.setTicket((Ticket) QueryDao.getVariableToProcessById(wfInfo.getInstanceId(), "ticket"));
	}
	
	public String getSelectedPartner() {
		return selectedPartner;
	}

	public void setSelectedPartner(String selectedPartner) {
		this.selectedPartner = selectedPartner;
	}

	public String getTypeOfDamage() {
		return typeOfDamage;
	}

	public void setTypeOfDamage(String typeOfDamage) {
		this.typeOfDamage = typeOfDamage;
	}

	public void handleDamageChange(){
		this.partners = Partners.getPartners(this.typeOfDamage);
	}
	
	public String finish() {
		//selected Partner speichern
		Partner selectedPartner = Partners.getPartnerByName(this.selectedPartner);
		Ticket ticket=(Ticket)QueryDao.getVariableToProcessById(wfInfo.getInstanceId(), "ticket");
		ticket.setSelectedPartner(selectedPartner);
		ticket.setTypeOfDamage(this.typeOfDamage);
		QueryDao.saveVariableToProcessById(wfInfo.getInstanceId(), "ticket", ticket);
		
		String nextPage = "myTasks";

		// Retrieving Activiti engine
		ActivitiManager manager = ActivitiManager.getInstance();

		try {
			manager.getTaskRuntimeService().complete(wfInfo.getTaskId());

		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}

		// Check for any execution errors
		String instanceId = wfInfo.getInstanceId();
		
		Task task=QueryDao.getNextTaskAndSetAssignee(instanceId, this.user);  //holt sich den nächsten Task zur Gruppe und setzt Asignee
		
		if(task!=null){
			wfInfo.setTaskId(task.getId());
			nextPage = QueryDao.getFormKey(wfInfo.getTaskId());
		}

		return WebUtils.redirect(nextPage);
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public WFInfo getWfInfo() {
		return wfInfo;
	}

	public void setWfInfo(WFInfo wfInfo) {
		this.wfInfo = wfInfo;
	}

	public List<Partner> getPartners() {
		return partners;
	}

	public void setPartners(List<Partner> partners) {
		this.partners = partners;
	}

	public Ticket getTicket() {
		return ticket;
	}

	public void setTicket(Ticket ticket) {
		this.ticket = ticket;
	}
	
	
	
}
